from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.response import Response
from user import serializers, models
from rest_framework.decorators import action


# Create your views here.
class UserViewSet(viewsets.ModelViewSet):
    queryset = models.User.objects.all()
    serializer_class = serializers.UserSer

    @action(detail=False, methods=['POST'])
    def login(self, request):
        print("########")
        phone = request.data.get('phone')
        email = request.data.get('email')
        password = request.data.get('password')
        phone_find = models.User.objects.filter(phone=phone).first()
        email_find = models.User.objects.filter(email=email).first()
        if phone_find or email_find:
            passwordone = models.User.objects.filter(password=password).first()
            if passwordone:
                return Response({"msg": "账号密码正确登录成功", "code": 200})
            else:
                return Response({"msg": "密码错误", "code": 400})
        else:
            return Response({"msg": "账号密码不存在请先注册", "code": 404})
